//
//  UIView+ZCorners.swift
//  AGF
//
//  Created by CS001 on 2021/3/15.
//

import UIKit
import BWToolKit

extension UIView {
    private struct AKeys{
        static var zCornersEnabled = "zCornersEnabled"
        static var zCornersRadii = "zCornersRadii"
        static var zCornersSets = "zCornersSets"
    }
    
    @objc public static func setupZCornersLoad() {
        let cls = Self.self
        Objcer.swizzleSelector1(#selector(layoutSubviews), inClass1: cls, withSelector2: #selector(sCornersLayoutSubviews), inClass2: cls)
    }
    
    @objc func sCornersLayoutSubviews(){
        sCornersLayoutSubviews()
        
        buildZCorners()
    }
    
    func buildZCorners(){
        guard zCornersEnabled else {
            return
        }
        var types: UIRectCorner = []
        let comps = zCornersSets.components(separatedBy: ",")
        for v in comps {
            guard let type = Self.cornersMap[v] else {
                continue
            }
            types.update(with: type)
        }
        
        let radii = comps.count>0 ? zCornersRadii : CGSize.zero;
        let layer = CAShapeLayer()
        layer.frame = bounds;
        layer.path = UIBezierPath(roundedRect: bounds, byRoundingCorners: types, cornerRadii: radii).cgPath
        self.layer.mask = layer;
    }
    
    public enum ZCornerSets: String {
        case allCorners = "0"
        case topLeft = "1"
        case topRight = "2"
        case bottomLeft = "3"
        case bottomRight = "4"
    }
    
    static let cornersMap: [String: UIRectCorner] = [
        "0": .allCorners,
        "1": .topLeft,
        "2": .topRight,
        "3": .bottomLeft,
        "4": .bottomRight
    ]
    
    public func setupZCorners(
        _ corners: Set<ZCornerSets>,
        radii: CGSize
    ){
        zCornersEnabled = true
        zCornersRadii = radii
        
        if corners.contains(.allCorners) {
            zCornersSets = ZCornerSets.allCorners.rawValue
        }
        else {
            zCornersSets = corners
                .filter{ $0 != .allCorners }
                .map{ $0.rawValue }
                .joined(separator: ",")
        }
        
        buildZCorners()
    }
    
    /// 是否开启指定圆角
    @IBInspectable var zCornersEnabled: Bool {
        get{
            return Objcer.getASValue(for: self, key: &AKeys.zCornersEnabled) as? Bool ?? false
        }
        set{
            Objcer.setASValue(newValue, for: self, key: &AKeys.zCornersEnabled, policy: .assign)
        }
    }
    /// 圆角大小: @"{10,10}"
    @IBInspectable var zCornersRadii: CGSize {
        get{
            let v = Objcer.getASValue(for: self, key: &AKeys.zCornersRadii) as! String
            return NSCoder.cgSize(for: v)
        }
        set{
            Objcer.setASValue(NSCoder.string(for: newValue), for: self, key: &AKeys.zCornersRadii, policy: .copy)
        }
    }
    /// 圆角设置：@"1=TopLeft, 2=TopRight, 3=BottomLeft, 4=BottomRight, 0=All"
    @IBInspectable var zCornersSets: String {
        get{
            return Objcer.getASValue(for: self, key: &AKeys.zCornersSets) as! String
        }
        set{
            Objcer.setASValue(newValue, for: self, key: &AKeys.zCornersSets, policy: .copy)
        }
    }
}
